package com.handy.controller.admin.setting;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.handy.config.ScheduleTaskConfig;
import com.handy.constants.ScheduleTaskEnum;
import com.handy.controller.BaseController;
import com.handy.entity.task.ScheduleTask;
import com.handy.service.task.IScheduleTaskService;
import com.handy.vo.PageBean;
import com.handy.vo.PageVo;
import com.handy.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 定时任务接口
 *
 * @author handy
 */
@RestController
@RequestMapping("/api/admin/setting/task")
@Api(tags = "定时任务")
@ApiSupport(author = "handy")
@RequiredArgsConstructor
public class ScheduleTaskApiController extends BaseController {
    private final IScheduleTaskService scheduleTaskService;
    private final ScheduleTaskConfig scheduleTaskConfig;

    @GetMapping("/list")
    @ApiOperation(value = "获取列表数据")
    public PageVo<ScheduleTask> list(PageBean pageBean) {
        LambdaQueryWrapper<ScheduleTask> wrapper = Wrappers.lambdaQuery(new ScheduleTask()).orderByDesc(ScheduleTask::getCreateTime);
        IPage<ScheduleTask> scheduleTaskPage = scheduleTaskService.page(new Page<>(pageBean.getPage(), pageBean.getLimit()), wrapper);
        return PageVo.create(scheduleTaskPage.getRecords(), scheduleTaskPage.getTotal());
    }

    @PutMapping("/confirm/edit/status")
    @ApiOperation(value = "是否启用状态更改")
    public ResultVo<?> confirmEditStatus(HttpSession session, @ApiParam(name = "id", value = "主键id") Long id, @ApiParam(name = "status", value = "启用状态") Boolean status) {
        ScheduleTask scheduleTask = new ScheduleTask();
        scheduleTask.setId(id);
        scheduleTask.setStatus(status);
        scheduleTask.setMender(getLoginUser(session).getCode());
        boolean rst = scheduleTaskService.updateById(scheduleTask);
        if (rst) {
            runTask();
        }
        return rst ? ResultVo.success("更新启用状态成功") : ResultVo.success("更新启用状态失败");
    }

    @DeleteMapping("/confirm/delete/{id}")
    @ApiOperation(value = "任务删除")
    public ResultVo<?> confirmDelete(@PathVariable Long id) {
        boolean rst = scheduleTaskService.removeById(id);
        if (rst) {
            runTask();
        }
        return rst ? ResultVo.success("删除成功") : ResultVo.failure("删除失败");
    }

    @PostMapping("/confirm/add")
    @ApiOperation(value = "任务新增")
    public ResultVo<?> confirmAdd(HttpSession session, ScheduleTask scheduleTask) {
        ScheduleTaskEnum scheduleTaskEnum = ScheduleTaskEnum.getEnum(scheduleTask.getClazzPathId());
        if (scheduleTaskEnum == null) {
            return ResultVo.failure("新增失败");
        }
        scheduleTask.setClazzPathName(scheduleTaskEnum.getPackageName());
        scheduleTask.setCreator(getLoginUser(session).getCode());
        boolean rst = scheduleTaskService.save(scheduleTask);
        if (rst) {
            runTask();
        }
        return rst ? ResultVo.success("新增成功") : ResultVo.failure("新增失败");
    }

    @PutMapping("/confirm/edit")
    @ApiOperation(value = "任务编辑")
    public ResultVo<?> confirmEdit(HttpSession session, ScheduleTask scheduleTask) {
        ScheduleTaskEnum scheduleTaskEnum = ScheduleTaskEnum.getEnum(scheduleTask.getClazzPathId());
        if (scheduleTaskEnum == null) {
            return ResultVo.failure("新增失败");
        }
        scheduleTask.setClazzPathName(scheduleTaskEnum.getPackageName());
        scheduleTask.setMender(getLoginUser(session).getCode());
        boolean rst = scheduleTaskService.updateById(scheduleTask);
        if (rst) {
            runTask();
        }
        return rst ? ResultVo.success("更新成功") : ResultVo.success("更新失败");
    }

    /**
     * 重新运行定时任务
     */
    private void runTask() {
        List<ScheduleTask> list = scheduleTaskService.list();
        scheduleTaskConfig.startCron(list);
    }

}
